package engine

import (
	"L.Go/src/crawler/fetcher"
	"log"
)

type SimpleEngine struct {
}

func (e SimpleEngine) Run(seeds ...Request) {
	var requests []Request
	for _, r := range seeds {
		requests = append(requests, r)
	}

	for len(requests) > 0 {
		r := requests[0]
		requests = requests[1:]

		parserResult, err := worker(r)
		if err != nil {
			continue
		}
		//body, err := fetcher.Fetcher(r.Url)
		//if err != nil {
		//	fmt.Println(err)
		//	continue
		//}
		//parserResult := r.ParserFunc(body)
		requests = append(requests, parserResult.Requests...)
		for _, item := range parserResult.Items {
			log.Printf("got item %v", item)
		}
	}
}

func  worker(r Request) (ParserResult, error) {
	log.Printf("Fetching %s", r.Url)
	body, err := fetcher.Fetcher(r.Url)
	if err != nil {
		log.Printf("Fetcher:error fetching url %s: %v", r.Url, err)
		return ParserResult{}, nil
	}
	return r.ParserFunc(body), nil

}
